package com.itcast.manager3.B_controller;

import com.itcast.manager2.E_domain.Student;
import com.itcast.manager3.C_service.StudentService;
import com.itcast.manager3.tomian.StudentTomain;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentContorller {
    private Scanner sc = new Scanner(System.in);
    private StudentService ss = new StudentService();
    public void start() {
        while (true) {
            System.out.println("---欢迎进入学生管理系统---");
            System.out.println("1：增加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查看学生");
            System.out.println("5：退出");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updeteStendt();
                    break;
                case 4:
                    findAllStendt();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }

        }
    }

    private void updeteStendt() {
        System.out.println("请输入您要修改的学生学号");
        String id = sc.next();
        if (getStudentrepetition(id)==false){
            System.out.println("管理系统里现在没有您输入的学号，请重新输入");
            return;
        }
        System.out.println("请输入您要修改的学生名字");
        String name =  sc.next();
        System.out.println("请输入您要修改的学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入您要修改的学生生日");
        String birthday = sc.next();
        StudentTomain stu = new StudentTomain(id,name,age,birthday);
        ss.updateStudent(stu);
        System.out.println("修改成功");
    }

    private void findAllStendt() {
        ArrayList<StudentTomain> list = ss.getAllStudent();
        if (list.size()==0){
            System.out.println("现在管理系统还没有学生数据");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            StudentTomain stu = list.get(i);
            System.out.println(stu.getId()+"--"+stu.getName()+"--"+stu.getAge()+"--"+stu.getBirthday());
        }
    }

    private void deleteStudent() {
        System.out.println("请输入要删除的学生学号");
        String  id = sc.next();
        if (getStudentrepetition(id)==false){
            System.out.println("没有该学生学号，请重新输入");
            return;
        }else {
            ss.deleteStudent(id);
            System.out.println("删除成功");
        }

    }


    public boolean getStudentrepetition(String id) {
        boolean flag = ss.isExists(id);
        if (flag==true){
            return true;
        }else {
            return false;
        }
    }
    public void addStudent(){
        System.out.println("请输入学生学号");
        String id = sc.next();
        if (getStudentrepetition(id) == true) {
            System.out.println("已有输入的学生学号，请重新输入");
            return;
        }
        System.out.println("请输入学生名字");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        StudentTomain stu = new StudentTomain(id, name, age, birthday);
        ss.addStudent(stu);
        System.out.println("添加成功");
    }

}
